<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay as LibraryPay;
use app\common\model\RechargeConfig;
use app\common\model\user\Rechargeoder;
use think\Db;

/**
 * 会员充值接口
 */
class Recharge extends Api
{
    protected $noNeedLogin = ['recharge_config'];
    protected $noNeedRight = ['*'];

    /**
     * 充值余额
     * @ApiMethod (get)
     * 
     */
    public function recharge_config()
    {
        $data = RechargeConfig::get(1);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (充值)
     * @ApiMethod (POST)
     * @ApiParams (name="money", type="integer", required=true, sample="", description="充值金额")
     * @ApiParams (name="send", type="integer", required=true, sample="", description="赠送金额")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function recharge()
    {
        $param = request()->param();
        extract($param);
        $data = [
            'orderid'   => sn('recharge_order', 'orderid', 'RZ'),
            'user_id'   => $this->auth->id,
            'amount'    => bcadd($money, $send, 2),
            'payamount' => $money,
        ];
        Db::startTrans();
        try {
            $order = new Rechargeoder($data);
            $order->allowField(true)->save();
            if ($order->id) {
                // $jsapi = LibraryPay::pay($order->id, 'recharge', $this->auth->getUser(), 1);
                queue('app/common/job/rechargeOrder', $order->id, 600, 'rechargeOrder1272');
            } else {
                exception('充值下单失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('支付失败：' . $e->getMessage());
        }
        $this->success('', $order->id);
    }

    /**
     * @Author LK
     * @ApiTitle (充值记录)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function logs()
    {
        $param = request()->param();
        extract($param);
        $lists = Rechargeoder::with('user')->where('pay_status', '10')->order('paytime desc')->paginate();
        $this->success('', $lists);
    }
}
